<?php

//------------Forms generated from formsWiz
include_once("../../globals.php");
include_once($GLOBALS["srcdir"] . "/api.inc");

/*function fixdate_report($date) {
    $temp_date = explode("-", $date);
    $new_date = $temp_date[2] . "/" . $temp_date[1] . "/" . $temp_date[0];
    return $new_date;
}*/

function examenes_report($pid, $encounter, $cols, $id) {
    $cols = 1; // force always 1 column
    $count = 0;
    $data = formFetch("form_examenes", $id);
    if ($data) {
        $result= sqlFetchArray(
                        sqlStatement("select * from form_examenes f where f.pid='$pid' and f.id='$id'")
                    );
        print "<table><tr>";
        /*echo "<pre>";
        print_r($data);
        echo "</pre>";*/
        foreach ($data as $key => $value) {

            if ($key == "id" || $key == "pid" || $key == "userid" || $key == "estado" || $key == "activity" || $key == "date" || $value == "" || $value == "0000-00-00 00:00:00") {
                continue;
            }

            switch ($key) {
                case 'tipoexamen':
                    
                    $key2 = 'tipoexamen';
                        $cres2 = sqlFetchArray(sqlStatement("SELECT * FROM `list_options` WHERE list_id = 'Tipos_examen' AND seq='" . $result{"tipoexamen"} . "' order by seq asc"));
                        print "<td><span class=bold> Examen realizado: </span><span class=text>" . $cres2{"title"} . "</span></td>";
                    break;
                case 'comentarios':
                    $key2 = 'Comentarios';
                        print "<td><span class=bold>" . $key2 . ": </span><span class=text>$value</span></td>";
                    break;

                case 'documentid':
                    $key2 = 'documentid';
                    break;
            }
            $key = ucwords(str_replace("_", " ", $key));
            
            /*else {
                print "<td><span class=bold>" . $key2 . ": </span><span class=text>$value</span></td>";
            }*/
            
            if ($key2 == 'documentid') {
         
               //LINK AL DOCUMENTO DEL EXAMEN DE LABORATORIO
                $query = " SELECT * FROM documents d  "
                        . " WHERE d.encounter= " . $_SESSION['encounter']
                        . " AND d.id=" . $result{"documentid"}
                        . " order by d.date desc ";

                $res = sqlStatement($query);
                while ($row = sqlFetchArray($res)) {
                    print "<span class='bold'>Archivo: </span><span class=text>" . basename($row['url']) . "</span><br/>";
                    //print "<span class='bold'>Fecha: </span><span class=text>" . fixdate_report_datetime($row['date']) . "</span><br/>";
                    print "<span class='bold'>Link de descarga: </span><span class=text>" . "<a href='../download.php?filename=" . $row['url'] . "' >Click aquí para descargar el archivo.</a>" . " </span><br/>";
                }
            }

            $count++;
            if ($count == $cols) {
                $count = 0;
                print "</tr><tr>\n";
            }
        }
    }
    print "</tr></table>";
}

?>
